import lejos.nxt.Motor;
import lejos.robotics.subsumption.Behavior;

/**
 * Avoid obstacles.
 */
public class Avoid implements Behavior {
	private boolean suppressed = false;
	private int detectionValue = 30;

	/**
	 * Make a turn when we take action.
	 */
	public void action() {
		suppressed = false;

		Motor.A.rotate(180, true);
		Motor.B.rotate(-180, true);

		while ((Motor.A.isMoving() || Motor.B.isMoving()) && !suppressed)
			Thread.yield();

		Motor.A.stop();
		Motor.B.stop();
	}
	
	/* (non-Javadoc)
	 * @see lejos.robotics.subsumption.Behavior#suppress()
	 */
	public void suppress() {
		suppressed = true;
	}

	/**
	 * Take control when ultrasonic data is under specific value.
	 */
	public boolean takeControl() {
		return (BillManager.GetUltrasonicData() < detectionValue);
	}
}
